﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace AIM.Common
{
    public static class Encrypt
    {
        /// <summary>
        /// 返回当前字符串的MD5编码
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static String Md5(this String self)
        {
            return BitConverter.ToString(System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.Default.GetBytes(self))).Replace("-", "");
        }

        /// <summary>
        /// 返回当前字符串的SHA1编码
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static String SHA1(this String self)
        {
            return BitConverter.ToString(System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.Default.GetBytes(self))).Replace("-", "");
        }

        /// <summary>
        /// 返回当前字符串的Base64编码
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static String Base64Encode(this String self)
        {
            return Convert.ToBase64String(Encoding.Default.GetBytes(self));
        }

        /// <summary>
        /// 解密当前的Base64编码
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static String Base64Decode(this String self)
        {
            return Encoding.Default.GetString(Convert.FromBase64String(self));
        }

        /// <summary>
        /// 对当前字符串应用DES加密
        /// </summary>
        /// <param name="self"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String DesEncode(this String self, String key)
        {
            var des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(self);
            key = key.Md5().Substring(0, 8);
            des.Key = Encoding.ASCII.GetBytes(key);
            des.IV = Encoding.ASCII.GetBytes(key);
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            var ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
                ret.AppendFormat("{0:X2}", b);
            return Convert.ToBase64String(Encoding.Default.GetBytes(ret.ToString()));
        }

        /// <summary>
        /// 对当前字符串应用DES解密
        /// </summary>
        /// <param name="self"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String DesDecode(this String self, String key)
        {
            try
            {
                self = Encoding.Default.GetString(Convert.FromBase64String(self));
                var des = new DESCryptoServiceProvider();
                var inputByteArray = new byte[self.Length / 2];
                for (int x = 0; x < self.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(self.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                key = key.Md5().Substring(0, 8);
                des.Key = Encoding.ASCII.GetBytes(key);
                des.IV = Encoding.ASCII.GetBytes(key);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {
                return String.Empty;
            }
        }
    }
}
